package com.ruoyi.erp.controller;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.erp.domain.Payroll;
import com.ruoyi.erp.service.IPayrollService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 员工每月成本Controller
 * 
 * @author sauger
 * @date 2022-03-10
 */
@Controller
@RequestMapping("/erp/payroll")
public class PayrollController extends BaseController
{
    private String prefix = "erp/payroll";

    @Autowired
    private IPayrollService payrollService;

    @RequiresPermissions("erp:payroll:view")
    @GetMapping()
    public String payroll()
    {
        return prefix + "/payroll";
    }

    /**
     * 查询员工每月成本列表
     */
    @RequiresPermissions("erp:payroll:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Payroll payroll)
    {
        startPage();
        List<Payroll> list = payrollService.selectPayrollList(payroll);
        return getDataTable(list);
    }

    /**
     * 导出员工每月成本列表
     */
    @RequiresPermissions("erp:payroll:export")
    @Log(title = "员工每月成本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Payroll payroll)
    {
        List<Payroll> list = payrollService.selectPayrollList(payroll);
        ExcelUtil<Payroll> util = new ExcelUtil<Payroll>(Payroll.class);
        return util.exportExcel(list, "员工每月成本数据");
    }

    /**
     * 导入成本数据
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/import")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Payroll> util = new ExcelUtil<Payroll>(Payroll.class);
        List<Payroll> list = util.importExcel(file.getInputStream());
        String operName = getLoginName();
        String message = payrollService.importData(list, operName);
        return AjaxResult.success(message);
    }
    /**
     * 新增员工每月成本
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存员工每月成本
     */
    @RequiresPermissions("erp:payroll:add")
    @Log(title = "员工每月成本", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Payroll payroll)
    {
        payroll.setCreateBy(getLoginName());
        payroll.setUpdateTime(DateUtils.getNowDate());
        payroll.setUpdateBy(getLoginName());
        return toAjax(payrollService.insertPayroll(payroll));
    }

    /**
     * 修改员工每月成本
     */
    @RequiresPermissions("erp:payroll:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        Payroll payroll = payrollService.selectPayrollById(id);
        mmap.put("payroll", payroll);
        return prefix + "/edit";
    }

    /**
     * 修改保存员工每月成本
     */
    @RequiresPermissions("erp:payroll:edit")
    @Log(title = "员工每月成本", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Payroll payroll)
    {
        return toAjax(payrollService.updatePayroll(payroll));
    }

    /**
     * 删除员工每月成本
     */
    @RequiresPermissions("erp:payroll:remove")
    @Log(title = "员工每月成本", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(payrollService.deletePayrollByIds(ids));
    }
}
